package ca.mcgill.mni.bic;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;

public class SeriesDescriptionSelector extends Dialog {
	
	List category;
	List seriesDescription;
		
	public SeriesDescriptionSelector (Shell parent, int style) {
		super (parent, style);
	}
	public SeriesDescriptionSelector (Shell parent) {
		this (parent, 0); // your default style bits go here (not the Shell's style bits)
	}
	public SeriesDescriptionSelector(Group group, int none) {
		// TODO Auto-generated constructor stub
		this (group.getShell(), none);
	}
	public String open () {
		Shell parent = getParent();
		Shell shell = new Shell(parent, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
		shell.setText("Series Description");
		
		shell.setLayout(new FillLayout(SWT.HORIZONTAL));
		
		final Config config = Config.singleton();
		
		final List category = new List(shell, SWT.BORDER);
		for (String categoryName : config.seriesDescriptions.keySet()) {
			category.add(categoryName);
		}
		category.addSelectionListener(new SelectionListener() {

			public void widgetDefaultSelected(SelectionEvent event) {
				populateDescriptions(config.seriesDescriptions.get(category.getSelection()[0]));
			}

			public void widgetSelected(SelectionEvent event) {
				populateDescriptions(config.seriesDescriptions.get(category.getSelection()[0]));
			}
			
		});
		
		seriesDescription = new List(shell, SWT.BORDER);
		
		shell.open();
		Display display = parent.getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) display.sleep();
		}
		return seriesDescription.getSelection()[0];
	}
	protected void populateDescriptions(java.util.List<String> descriptions) {
		seriesDescription.removeAll();
		
		for(String description : descriptions) {
			seriesDescription.add(description);
		}
	}
 }